package com.base.cn.platform.os.controller.classes.task;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.classes.TeacherDataIsolation;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.exam.paper.ExamPaper;
import com.base.cn.platform.os.entity.exam.record.ExamRecordCondition;
import com.base.cn.platform.os.entity.exam.record.UserScoreRanking;
import com.base.cn.platform.os.service.course.classes.ClassesService;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import com.base.cn.platform.os.service.exam.paper.ExamPaperService;
import com.base.cn.platform.os.service.exam.record.ExamRecordService;
import com.base.cn.platform.os.service.exam.record.LoadStartExamService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班级作业
 *
 * @author SK
 * @since 2018-04-26
 */
@Controller
@RequestMapping("/admin/classes/task")
public class AdminClassesTaskController extends AdminBaseController {
    @Autowired
    private ExamPaperService examPaperService;
    @Autowired
    private ExamRecordService examRecordService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private LoadStartExamService loadStartExamService;

    /**
     * 初始化班级作业列表页面
     *
     * @return 班级作业列表页面
     */
    @RequestMapping("/taskList")
    public String taskList() {
        return resultPage("/classes/task/task-list");
    }

    /**
     * 班级作业列表页面
     *
     * @return 班级作业列表页面
     */
    @RequestMapping("/ajaxTaskListPage")
    @TeacherDataIsolation(type = 5)
    public String ajaxPaperListPage(Model model,
                                    @RequestBody Map<String, Object> condition,
                                    @RequestParam("currentPage") int currentPage) {
        condition.put("subjectData", true);
        condition.put("createUserData", true);
        condition.put("examTypeData", true);
        condition.put("courseData", true);
        condition.put("classesData", true);
        condition.put("useType", 2);//只查询作业类型的
        condition.put("notStatus", "3");
        Map<String, Object> dataMap = examPaperService.findExamPaperListPage(condition, currentPage, 15);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/classes/task/ajax-task-list");
    }

    /**
     * 班级作业列表页面只查询本班的
     *
     * @return 班级作业列表页面
     */
    @RequestMapping("/ajaxClassesTaskListPage")
    @TeacherDataIsolation(type = 5)
    public String ajaxClassesTaskListPage(Model model,
                                          @RequestBody Map<String, Object> condition,
                                          @RequestParam("currentPage") int currentPage) {
        condition.put("subjectData", true);
        condition.put("createUserData", true);
        condition.put("examTypeData", true);
        condition.put("courseData", true);
        condition.put("classesData", true);
        condition.put("useType", 2);//只查询作业类型的
        condition.put("notStatus", "3");
        Map<String, Object> dataMap = examPaperService.findExamPaperListPage(condition, currentPage, 15);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/classes/task/ajax-classes-task-list");
    }

    /**
     * 查询班级内的作业列表
     * @param request
     * @param model
     * @param taskId
     * @return
     */
    @RequestMapping("/classesTaskInfoListPage")
    public String classesTaskInfoListPage(HttpServletRequest request, Model model, @RequestParam("taskId") BigDecimal taskId) {
        request.setAttribute("taskId", taskId);
        Map<String, Object> condition = new HashMap<>();
        condition.put("examId", taskId);
        condition.put("useType", 2);
        condition.put("status", 2);
//        model.addAttribute("finishCount", examRecordService.getExamRecordCount(condition));
        model.addAttribute("finishCount",0);
        condition.put("status", "");
//        model.addAttribute("taskCount", examRecordService.getExamRecordCount(condition));
        model.addAttribute("taskCount", 0);
        condition = new HashMap<>();
        condition.put("id", taskId);
        condition.put("classesData", true);
        ExamPaper examPaper = examPaperService.findExamPaperById(condition);
        model.addAttribute("examPaper", examPaper);
        condition.put("examId", taskId);
        List<UserScoreRanking> examRecordList = examRecordService.queryUserScoreRanking(condition);
        if (ObjectUtils.isNotEmpty(examRecordList)) {
            model.addAttribute("maxCount", examRecordList.get(0).getUserScore());
            model.addAttribute("minCount", examRecordList.get(examRecordList.size() - 1).getUserScore());
        }
        return resultPage("/classes/task/classes-task-info-list");
    }

    /**
     * 查询班级内的作业列表ajax
     *
     * @param model
     * @param condition
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxClassesTaskInfoListPage")
    public String ajaxClassesTaskInfoListPage(Model model,
                                              @RequestBody Map<String, Object> condition,
                                              @RequestParam("currentPage") int currentPage) {

        condition.put("userData", true);
        condition.put("examPaperData", true);
        condition.put("useType", 2);
        condition.put("rankingData", true);
        Map<String, Object> dataMap = examRecordService.findExamRecordListPage(condition, currentPage, 12);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/classes/task/ajax-classes-task-info-list");
    }

    /**
     * 作业批阅
     * @param model
     * @param examRecordId
     * @param type
     * @return
     */
    @RequestMapping({"/userTaskMarking"})
    public String userTaskMarking(Model model,
                                 @RequestParam("examRecordId") BigDecimal examRecordId,@RequestParam(value = "type",required = false) Integer type){

        Map<String,Object> condition = new HashMap<>();
        condition.put("id",examRecordId);
        condition.put("userId",BigDecimal.ZERO);
        model.addAttribute("type",type);
        Map<String,Object> resultMap = loadStartExamService.loadExamRecord(condition);
        model.addAttribute("examRecord", ResultUtil.getEntity(resultMap));
        return resultPage("/classes/task/task-record-info");
    }
}
